package com.it.mydream.Controller;

import com.it.mydream.Bean.MsgBoard;
import com.it.mydream.Bean.User;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class VideoController {

    @RequestMapping("changevideo")
    @ResponseBody
    public String changeVideo(HttpServletRequest request){
        String video_path = request.getParameter("videopath");
        ServletContext application = request.getServletContext();
        application.setAttribute("videopath","https://www.bilibili.com/video/"+video_path);
        return "1";
    }

    @RequestMapping("getvideopath")
    @ResponseBody
    public String getVideopath(HttpServletRequest request){
        ServletContext application = request.getServletContext();
        String videopath = (String) application.getAttribute("videopath");
        return videopath;
    }

    @RequestMapping("sendtalk")
    @ResponseBody
    public String sendTalk(HttpServletRequest request){
        String talk = request.getParameter("talk");
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        MsgBoard talkboard = new MsgBoard();
        talkboard.setUser(user);
        talkboard.setMsg_content(talk);
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        talkboard.setMsg_time(sdf.format(System.currentTimeMillis()));

        ServletContext application = request.getServletContext();
        List<MsgBoard> talklista = (List<MsgBoard>) application.getAttribute("talklist");
        List<MsgBoard> talklists = (List<MsgBoard>) session.getAttribute("talklist");
        if(talklista==null){
            talklista = new ArrayList<MsgBoard>();
            talklista.add(talkboard);
            application.setAttribute("talklist",talklista);
        }else{
            talklista.add(talkboard);
            application.setAttribute("talklist",talklista);
        }
        if(talklists==null){
            talklists = new ArrayList<MsgBoard>();
            session.setAttribute("talklist",talklists);
        }
        return "1";
    }

    @RequestMapping("gettalk")
    @ResponseBody
    public Map<String,Object> getTalk(HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
        List<MsgBoard> talks = new ArrayList<MsgBoard>();
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();
        List<MsgBoard> talklista = (List<MsgBoard>) application.getAttribute("talklist");
        List<MsgBoard> talklists = (List<MsgBoard>) session.getAttribute("talklist");
        if(talklista==null) {
            map.put("talks",talks);
            return map;
        }else{
            if(talklists==null){
                talklists= new ArrayList<MsgBoard>();
            }else{
                if(talklists.size()==talklista.size()){
                    map.put("talks",talks);
                    return map;
                }
            }
            for(int i=talklists.size();i<talklista.size();i++){
                talklists.add(talklista.get(i));
                talks.add(talklista.get(i));
            }
        }
        map.put("talks",talks);
        session.setAttribute("talklist",talklists);
        return map;
    }
}
